<%@include file="template/configuration.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${website} || ${str_folder}</title>
        ${stylesheet}
        ${jquery}
        ${jspagination}
    </head>
    <body>
        <div class="container">
            <%@include file="template/header.jsp" %>
            <div class="content">
                <h1>${str_folder}</h1>
                <div class="mail-block">
                    <c:choose>
                        <c:when test="${fn:length(private_messages)>0}">
                            <c:if test="${folder == 'trash'}">
                                <div class="button float-left" style="margin-bottom: 10px;">
                                    <a href="PrivateMessage?folder=trash&empty=true" class="remove">Empty trash</a>
                                    <span class="info">all <strong>Private messages</strong> from trash</span>
                                </div>
                            </c:if>
                            <table class="overview">
                                <tr>
                                    <th class="single-icon"></th>
                                    <th>Subject</th>
                                    <th>
                                        <c:choose>
                                            <c:when test="${folder == 'outbox'}">
                                                To
                                            </c:when>
                                            <c:when test="${folder == 'trash'}">
                                                Folder
                                            </c:when>
                                            <c:otherwise>
                                                From
                                            </c:otherwise>
                                        </c:choose>
                                    </th>
                                    <th class="date">Date</th>
                                    <th class="<c:choose><c:when test="${folder != 'outbox'}">manage</c:when><c:otherwise>single-icon</c:otherwise></c:choose>">Actions</th>
                                </tr>
                                <c:set var="i" value="0"/>
                                <c:forEach items="${private_messages}" var="message">
                                    <c:set var="i" value="${i + 1}"/>
                                    <tr class="pointer <c:choose><c:when test="${i % 2 == 0}">even</c:when><c:otherwise>odd</c:otherwise></c:choose>">
                                        <td>
                                            <a href="${url}ComposePrivateMessage?message=${message.id}&action=view" title="View">
                                                <c:choose>
                                                    <c:when test="${message.status == 1}">
                                                        <img src="${url}images/mail-b-closed.png" />
                                                    </c:when>
                                                    <c:otherwise>
                                                        <img src="${url}images/mail-b-open.png" />
                                                    </c:otherwise>
                                                </c:choose>
                                            </a>
                                        </td>
                                        <td <c:if test="${message.status == 1}">style="color: #1a344b; font-weight: bold;"</c:if>>
                                            <a href="${url}ComposePrivateMessage?message=${message.id}&action=view" title="View">${message.subject}</a>
                                        </td>
                                        <td>
                                            <c:choose>
                                                <c:when test="${folder == 'outbox'}">
                                                    ${message.receiver.firstname} ${message.receiver.insertion} ${message.receiver.lastname}
                                                </c:when>
                                                <c:when test="${folder == 'trash'}">
                                                    <c:choose>
                                                        <c:when test="${message.sender.member_id == sessionScope.userdetails.id}">
                                                            Outbox
                                                        </c:when>
                                                        <c:otherwise>
                                                            Inbox
                                                        </c:otherwise>
                                                    </c:choose>
                                                </c:when>
                                                <c:otherwise>
                                                    ${message.sender.firstname} ${message.sender.insertion} ${message.sender.lastname}
                                                </c:otherwise>
                                            </c:choose>
                                        </td>
                                        <td>${message.date}</td>
                                        <td>
                                            <a href="${url}ComposePrivateMessage?message=${message.id}&action=view" title="View" class="hidden"><img src="${url}images/view.png" /></a>
                                            <c:if test="${folder == 'inbox'}">
                                                <a href="${url}ComposePrivateMessage?message=${message.id}&action=reply" title="Reply"><img src="${url}images/mail-reply.png" /></a>
                                            </c:if>
                                            <c:if test="${folder == 'trash'}">
                                                <a href="${url}ComposePrivateMessage?message=${message.id}&action=revert" title="Revert"><img src="${url}images/mail-revert.png" /></a>
                                            </c:if>
                                            <a href="${url}ComposePrivateMessage?message=${message.id}&action=delete" title="Remove" class="remove"><img src="${url}images/remove.png" /></a>
                                            <c:choose>
                                                <c:when test="${folder == 'trash'}">
                                                    <span class="info">this message definitely from <strong>${message.sender.firstname} ${message.sender.insertion} ${message.sender.lastname}</strong><br />(This action cannot not be undone)</span>
                                                </c:when>
                                                <c:otherwise>
                                                    <span class="info">this message from <strong>${message.sender.firstname} ${message.sender.insertion} ${message.sender.lastname}</strong></span>
                                                </c:otherwise>
                                            </c:choose>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </table>
                            <%
                            HibernateUtil hupms = new HibernateUtil();
                            PrivateMessageDao cdpms = new PrivateMessageDao(hupms.openSession());
                            User user = (User) request.getSession().getAttribute("user");
                            double total_items = cdpms.getLastPage(request.getAttribute("folder").toString(), user.getMember_id());
                            pageContext.setAttribute("pagination", (int) Math.ceil(total_items / 15));
                            %>
                            <c:if test="${pagination > 1}">
                                <div class="pagination">
                                    <c:forEach var="i" begin="1" end="${pagination}" step="1">
                                        <a href="${url}jsp/pagination/private_messages.jsp?page=${i}&folder=${folder}" class="number<c:if test="${i == '1'}"> active</c:if>">${i}</a>
                                    </c:forEach>
                                </div>
                            </c:if>
                        </c:when>
                        <c:otherwise>
                            There are no messages in ${folder} yet.
                        </c:otherwise>
                    </c:choose>
                </div>
                <div class="navigation-mail-block">
                    <div class="folder">
                        <a href="${url}PrivateMessage?folder=inbox" <c:if test="${folder == 'inbox'}">class="bold"</c:if>>Inbox</a>
                    </div>
                    <div class="folder">
                        <a href="${url}PrivateMessage?folder=outbox" <c:if test="${folder == 'outbox'}">class="bold"</c:if>>Outbox</a>
                    </div>
                    <div class="folder">
                        <a href="${url}PrivateMessage?folder=trash" <c:if test="${folder == 'trash'}">class="bold"</c:if>>Trash</a>
                    </div>
                </div>
            </div>
            <%@include file="template/footer.jsp" %>
        </div>
    </body>
</html>
